package queue;

import iterator.Iterator;
import visitor.Visitor;
import wrapper.Comparable;
import container.AbstractContainer;
import container.Container;
import container.ContainerEmptyException;
import container.ContainerFullException;
import enumeration.Enumeration;

public class QueueAsArray extends AbstractContainer implements Queue {
	protected Object[] array;
	protected int head;
	protected int tail;

	public QueueAsArray(int size) {
		array = new Object[size];
		head = 0;
		tail = size - 1;
	}

	@Override
	protected int compareTo(Comparable arg) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object dequeue() {
		try {
			if (count == 0)
				throw new ContainerEmptyException("A fila está vazia.");
		} catch (ContainerEmptyException e) {
			e.printStackTrace();
		}
		Object result = array[head];
		array[head] = null;
		if(++head == array.length)
			head = 0;
		--count;
		return result;
	}

	@Override
	public void enqueue(Object object) {
		try {
			if (count == array.length)
				throw new ContainerFullException("A fila está cheia.");
		} catch (ContainerFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(++tail == array.length)
			tail = 0;
		array[tail] = object;
		++count;
	}

	@Override
	public Object getHead() {
		try {
			if (count == 0)
				throw new ContainerEmptyException("A fila está vazia.");
		} catch (ContainerEmptyException e) {
			e.printStackTrace();
		}
		return array[head];
	}

	@Override
	public void accept(Visitor visitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public Enumeration getEnumeration() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterator getIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	public void purge() {
		while (count > 0) {
			array[head] = null;
			if (++head == array.length)
				head = 0;
			--count;
		}
	}
}
